CC := $(CROSS_COMPILE)gcc
CFLAGS := -Wall -Wextra -O2

# 处理额外include path
INCLUDE_PATHS += app
INCLUDE_PATHS += daemon
INCLUDE_PATHS += ota
INCLUDE_PATHS += thirdparty

CFLAGS += $(addprefix -I, $(INCLUDE_PATHS))

# 处理额外lib
EXT_LIB += paho-mqtt3c
EXT_LIB += curl
EXT_LIB += crypto
EXT_LIB += rt

CFLAGS += $(addprefix -l, $(EXT_LIB))

# 处理交叉编译sysroot
ifdef CROSS_COMPILE
CFLAGS += --sysroot=$(SYSROOT)
endif

SRC := thirdparty/log.c/log.c \
       thirdparty/cJSON/cJSON.c \
	   app/app_buffer.c \
	   app/app_task.c \
	   app/app_common.c \
	   app/app_device.c \
	   app/app_serial.c \
	   app/app_bluetooth.c \
	   app/app_message.c \
	   app/app_mqtt.c \
	   app/app_router.c \
	   app/app_runner.c \
	   daemon/daemon_runner.c \
	   daemon/daemon_subprocess.c \
	   ota/ota_http.c \
	   ota/ota_update.c

OBJ := $(SRC:.c=.o)

TARGET := atguigu-gateway

PEER := root@172.17.0.27
INSTALL_DIR := /usr/bin

.PHONY: all clean cross

all: $(TARGET)

clean:
	$(RM) $(TARGET) main.o test.o $(OBJ)

cross:
	make clean
	CROSS_COMPILE=/home/atguigu/atguigupi/toolchain/bin/arm-linux-gnueabihf- SYSROOT=sysroot make all -j$(nproc)
	scp -O $(TARGET) $(PEER):$(INSTALL_DIR)
	scp -O initd/S99atguigu-gateway $(PEER):/etc/init.d/

$(TARGET): main.o $(OBJ)
	$(CC) -o $@ $^ $(CFLAGS)

test: test.o $(OBJ)
	$(CC) -o $@ $^ $(CFLAGS)
	-./$@
	$(RM) $@ $^